#include <windows.h>
#include <strsafe.h>
#include "resource.h"
#define MYWM_NOTIFYICON WM_USER + 1
HINSTANCE g_hInstance;
INT_PTR Dlg_Command(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	return (EndDialog(hwndDlg,IDOK));
}
void On_MYWM_NOTIFYICON(WPARAM wParam, LPARAM lParam) 
{	
	UINT uID; 
    UINT uMouseMsg; 
 
    uID = (UINT) wParam; 
    uMouseMsg = (UINT) lParam; 
 
    if (uMouseMsg == WM_LBUTTONDOWN) 
    { 
		MessageBox(NULL,TEXT("RECEICVE THE MESSAGE"),TEXT("MSG"),MB_OK);
	}

 }
BOOL MyTaskBarAddIcon(HWND hwnd, UINT uID, HICON hicon, LPSTR lpszTip) 
{ 
    BOOL res; 
    NOTIFYICONDATA tnid; 
 
    tnid.cbSize = sizeof(NOTIFYICONDATA); 
    tnid.hWnd = hwnd; 
    tnid.uID = uID; 
    tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; 
    tnid.uCallbackMessage = MYWM_NOTIFYICON; 
    tnid.hIcon = hicon; 
    if (lpszTip) 
        /*hr =*/ StringCbCopyN(tnid.szTip, sizeof(tnid.szTip), lpszTip, 
                           sizeof(tnid.szTip));
        // TODO: Add error handling for the HRESULT.
    else 
        tnid.szTip[0] = (TCHAR)'\0'; 
 
    res = Shell_NotifyIcon(NIM_ADD, &tnid); 
 
    if (hicon) 
        DestroyIcon(hicon); 
 
    return res; 
}
INT_PTR Dlg_Init(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	HICON hIcon = LoadIcon(g_hInstance,MAKEINTRESOURCE(IDI_ICON1));
	MyTaskBarAddIcon(hwndDlg,0,hIcon,TEXT("ICON"));
	return false;
}
BOOL MyTaskBarDeleteIcon(HWND hwnd, UINT uID) 
{ 
    BOOL res; 
    NOTIFYICONDATA tnid; 
 
    tnid.cbSize = sizeof(NOTIFYICONDATA); 
    tnid.hWnd = hwnd; 
    tnid.uID = uID; 
         
    res = Shell_NotifyIcon(NIM_DELETE, &tnid); 
    return res; 
}
INT_PTR CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_CONTEXTMENU:
		MessageBox(hwndDlg,TEXT("THE CONTXT MENU!@"),TEXT("MSG"),MB_OK);
		break;
	case MYWM_NOTIFYICON:
		On_MYWM_NOTIFYICON( wParam,  lParam);
		break;
	case WM_INITDIALOG:
		Dlg_Init(hwndDlg, uMsg,wParam, lParam);
		break;
	case WM_COMMAND:
		Dlg_Command(hwndDlg,uMsg, wParam, lParam);
		break;
	case WM_CLOSE:
		MyTaskBarDeleteIcon( hwndDlg,0);
		break;
	default:
		break;
	}
	return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	g_hInstance = hInstance;
	DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DialogProc);
}
